﻿using QxFramework.Utilities;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MapData : MonoSingleton<MapData>
{
    public readonly Dictionary<Vector3, CubeData> Cubes = new Dictionary<Vector3, CubeData>();
    public readonly Dictionary<Vector3, Dictionary<Vector3,DiceData>> Dices = new Dictionary<Vector3, Dictionary<Vector3, DiceData>>();
    /// <summary>
    /// 注册方块
    /// </summary>
    /// <param name="index"></param>
    /// <param name="cube"></param>
    /// <returns></returns>
    public bool Registe(CubeData cube)
    {
        if (!Cubes.ContainsKey(cube.Index))
        {
            Cubes.Add(cube.Index, cube);
            //Debug.Log("方块坐标注册！" + cube.Index);
            return true;
        }
        else
        {
            Debug.LogWarning("方块坐标已注册！" + cube.Index);
            return false;
        }
    }
    /// <summary>
    /// 注册面
    /// </summary>
    /// <param name="index"></param>
    /// <param name="suf"></param>
    /// <returns></returns>
    public bool Registe(SurfaceData suf)
    {
        if (!Cubes.ContainsKey(suf.Cube.Index))
        {
            Cubes.Add(suf.Index, suf.Cube);
        }
        if (!Cubes[suf.Cube.Index].Surfaces.ContainsKey(suf.Index))
        {
            Cubes[suf.Cube.Index].Surfaces.Add(suf.Index, suf);
            //Debug.Log("面坐标注册！" + suf.Cube.Index + "" + suf.Index);
            return true;
        }
        else
        {
            Debug.LogWarning("面坐标已注册！" + suf.Cube.Index + "" + suf.Index);
            return false;
        }
    }
    /// <summary>
    /// 注册骰子
    /// </summary>
    /// <param name="diceIndex"></param>
    /// <param name="dice"></param>
    /// <returns></returns>
    public bool Registe(DiceData dice)
    {
        if (dice.Surface != null)
        {
            if (!Cubes.ContainsKey(dice.Surface.Cube.Index))
            {
                Cubes.Add(dice.Surface.Cube.Index, dice.Surface.Cube);
            }
            if (!Cubes[dice.Surface.Cube.Index].Surfaces.ContainsKey(dice.Surface.Index))
            {
                Cubes[dice.Surface.Cube.Index].Surfaces.Add(dice.Surface.Index, dice.Surface);
            }
            if (!Cubes[dice.Surface.Cube.Index].Surfaces[dice.Surface.Index].Dices.ContainsKey(dice.DiceCoordinate))
            {
                Cubes[dice.Surface.Cube.Index].Surfaces[dice.Surface.Index].Dices.Add(dice.DiceCoordinate, dice);
                //Debug.Log("骰子坐标注册！" + dice.DiceCoordinate);
                return true;
            }
            else
            {
                Debug.LogWarning("坐标注册错误！" + dice.DiceCoordinate);
            }
        }
        else
        {
            if (!Dices.ContainsKey(dice.DiceCoordinate))
            {
                Dices.Add(dice.DiceCoordinate, new Dictionary<Vector3, DiceData>() { { dice.GroundTowards, dice } });
            }
            else
            {
                Dices[dice.DiceCoordinate].Add(dice.GroundTowards, dice);
            }
            return true;
        }
        return false;
    }
    /// <summary>
    /// 注册物体
    /// </summary>
    /// <param name="dice"></param>
    /// <returns></returns>
    public bool Registe(GameObject obj)
    {
        if (obj.transform.parent)
            if (obj.transform.parent.parent)
                if (obj.transform.parent.parent.tag == "Dice")
                {
                    obj.transform.parent.parent.GetComponent<DiceController>().FlushChild(obj);
                    return true;
                }
        Debug.LogWarning("物体注册错误！" + obj.name);
        return false;
    }
    /// <summary>
    /// 注销方块
    /// </summary>
    /// <param name="index"></param>
    /// <param name="cube"></param>
    /// <returns></returns>
    public bool Unregiste(CubeData cube)
    {
        if (Cubes.ContainsKey(cube.Index))
        {
            if (Cubes[cube.Index] == cube)
            {
                Cubes.Remove(cube.Index);
                return true;
            }
        }
        //Debug.LogError("方块坐标注销错误！" + cube.Index);
        return false;
    }
    /// <summary>
    /// 注销面
    /// </summary>
    /// <param name="index"></param>
    /// <param name="suf"></param>
    /// <returns></returns>
    public bool Unregiste(SurfaceData suf)
    {
        if (Cubes.ContainsKey(suf.Cube.Index))
        {
            if (Cubes[suf.Cube.Index].Surfaces.ContainsKey(suf.Index))
            {
                if (Cubes[suf.Cube.Index].Surfaces[suf.Index] == suf)
                {
                    Cubes[suf.Cube.Index].Surfaces.Remove(suf.Index);
                    if (Cubes[suf.Cube.Index].Surfaces.Count == 0)
                    {
                        Unregiste(suf.Cube);
                    } // 方块上没面了，注销方块
                    return true;
                }
            }
        }
        //Debug.LogError("面坐标错误！" + suf.Cube.Index + "" + suf.Index);
        return false;
    }
    /// <summary>
    /// 注销骰子
    /// </summary>
    /// <param name="index"></param>
    /// <param name="dice"></param>
    /// <returns></returns>
    public bool Unregiste(DiceData dice)
    {
        if (dice.Surface != null)
        {
            if (Cubes.ContainsKey(dice.Surface.Cube.Index))
                if (Cubes[dice.Surface.Cube.Index].Surfaces.ContainsKey(dice.Surface.Index))
                    if (Cubes[dice.Surface.Cube.Index].Surfaces[dice.Surface.Index].Dices.ContainsKey(dice.DiceCoordinate))
                        if (Cubes[dice.Surface.Cube.Index].Surfaces[dice.Surface.Index].Dices[dice.DiceCoordinate] == dice)
                        {
                            Cubes[dice.Surface.Cube.Index].Surfaces[dice.Surface.Index].Dices.Remove(dice.DiceCoordinate);
                            //if (Cubes[dice.Surface.Cube.Index].Surfaces[dice.Surface.Index].Dices.Count == 0)
                            //{
                            //    Unregiste(dice.Surface);
                            //} // 面上没骰子了，注销面
                            return true;
                        }
        }
        else
        {
            if (Dices.ContainsKey(dice.DiceCoordinate))
                if (Dices[dice.DiceCoordinate].ContainsKey(dice.GroundTowards))
                    Dices[dice.DiceCoordinate].Remove(dice.GroundTowards);
        }
        //Debug.LogError("骰子注销错误！" + dice.DiceCoordinate);
        return false;
    }
    /// <summary>
    /// 注销物体
    /// </summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    public bool Unregiste(GameObject obj)
    {
        if (obj.transform.parent)
            if (obj.transform.parent.parent)
                if (obj.transform.parent.parent.tag == "Dice")
                {
                    if (obj.transform.parent.parent.GetComponent<DiceController>().Data.ObjectOfGround == obj)
                    {
                        obj.transform.parent.parent.GetComponent<DiceController>().SetObjectOfGround();
                        return true;
                    }
                }
        Debug.LogWarning("注销出错！" + obj.name);
        return false;
    }
}
///// <summary>
///// 根据坐标获取方块
///// </summary>
///// <param name="index"></param>
///// <returns></returns>
//public static GameObject Dice(Vector3 index)
//{
//    if (map.ContainsKey(index))
//    {
//        return map[index];
//    }
//    else return null;
//}
/// <summary>
/// 注册
/// </summary>
/// <param name="index"></param>
/// <param name="dice"></param>
/// <returns></returns>
/// 
///// <summary>
///// 方块换坐标
///// </summary>
///// <param name="from"></param>
///// <param name="to"></param>
///// <param name="dice"></param>
///// <returns></returns>
//public static bool ChangePos(Vector3 from, Vector3 to, GameObject dice)
//{
//    if (map.ContainsKey(from))
//    {
//        if (map[from] == dice)
//        {
//            map.Remove(from);
//            map.Add(to, dice);
//            return true;
//        }
//        else
//        {
//            Debug.LogError("坐标与方块不对应！" + from + dice);
//        }
//    }
//    else
//    {
//        if (map.ContainsValue(dice))
//        {
//            Debug.LogError("初始坐标错误！" + from);
//        }
//        else
//        {
//            Debug.LogError("未登记此方块！" + dice);
//        }
//    }
//    return false;
//}
///// <summary>
///// 获取DiceData，如果没有，则返回空。
///// </summary>
///// <param name="index">坐标</param>
///// <returns></returns>
//public static DiceData GetDiceData(Vector3 index)
//{
//    if (map.ContainsKey(index))
//    {
//        return map[index].GetComponent<CubeController>().Data;
//    }
//    else return null;
//}
///// <summary>
///// 获取DiceData，如果没有，则返回空。
///// </summary>
///// <param name="index">坐标</param>
///// <returns></returns>
//public static DiceData GetDiceData(GameObject target)
//{
//    if (target != null)
//    {
//        return target.GetComponent<CubeController>().Data;
//    }
//    else return null;
//}

public class CubeData
{
    public MapData Map = MapData.Instance;
    public GameObject Obj;
    public Vector3 Index;
    public Dictionary<Vector3, SurfaceData> Surfaces;
    public CubeController Controller;
}

public class SurfaceData
{
    public CubeData Cube;
    public GameObject Obj;
    public Vector3 Index;
    public Dictionary<Vector3, DiceData> Dices;
    public SurfaceController Controller;
}

//public class DiceData
//{
//    public SurfaceData Surface;
//    public GameObject obj;
//    public Vector3 DiceCoordinate;
//    public Vector3 GroundTowards;
//    public GameObject ObjectOfGround;
//    private DiceController controller;
//    public int Set;
//    public DiceController Controller
//    {
//        get
//        {
//            return controller;
//        }

//        set
//        {

//            Set = value==null?1:2 ;
            
//            controller = value;
//        }
//    }

//}